﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Brc_PaaS.Api.Common;
using Brc_PaaS.Bll.RabbitMq;
using Brc_PaaS.Bll.System;
using Brc_PaaS.Common.Model.Basic;
using Brc_PaaS.Common.Model.RabbitMq;
using Exceptionless;
using Exceptionless.Logging;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace Brc_PaaS.Api.Controllers.RabbitMq
{
    /// <summary>
    /// 消息队列接口控制器
    /// </summary>
    [Route("api/RabbitMq/[controller]")]
    [ApiController]
    [EnableCors("any")]
    [ApiExplorerSettings(GroupName = "Exceptionless")]
    public class RabbitMqController : BaseController
    {
        private IRabbitMqManager _rabbitMqManager;
        private RabbitMQSettingEntity _entity;
        private IRabbitMqBasicManager _rabbitMqBasicManager;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="system"></param>
        /// <param name="rabbitMqManager"></param>
        /// <param name="rabbitMqBasicManager"></param>
        public RabbitMqController(IConfiguration configuration, ISystem system, IRabbitMqManager rabbitMqManager,IRabbitMqBasicManager rabbitMqBasicManager) : base(configuration, system)
        {
            this._rabbitMqManager = rabbitMqManager;
            this._rabbitMqBasicManager = rabbitMqBasicManager;
            _entity = configuration.GetSection("RabbitMQSettingEntity").Get<RabbitMQSettingEntity>();
        }


        /// <summary>
        /// 发送消息(测试使用，测试使用)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<BaseResponseModel> Post(RabbitMqMessageViewDto rabbitMqMessageViewDto)
        {
          
            BaseResponseModel result = new BaseResponseModel() { IsSuccess = true, Msg = string.Empty };
            if (SystemInfo == null)
            {
                result.Msg = "未能获取到对应业务系统信息，请联系管理员";
                return Ok(result);
            }

            var eventMessage = new EventMessage()
            {
                MessageMark = SystemInfo.Id,
                Message = rabbitMqMessageViewDto.Message,
                MessageId = rabbitMqMessageViewDto.MessageId,
                MessageCreatedTime = DateTime.Now
            };
            var rabbitMqInfo = _rabbitMqBasicManager.GetRabbitMqBySystemId(SystemInfo.Id);
            if(rabbitMqInfo==null || !rabbitMqInfo.IsSuccess|| rabbitMqInfo.Data==null)
            {
                result.Msg = "未能获取到对应业务系统的RabbitMq信息，请联系管理员";
                return Ok(result);
            }
            _entity.UserName = rabbitMqInfo.Data.UserName;
            _entity.Password = rabbitMqInfo.Data.Password;
            _entity.VirtualHost = rabbitMqInfo.Data.VirtualHost;
            _entity.RouteKey = rabbitMqMessageViewDto.RouteKey;
            _entity.ExchangeName = rabbitMqMessageViewDto.ExchangeName;
            _entity.ExchangeType = rabbitMqMessageViewDto.ExchangeType;
            result = _rabbitMqManager.SendMessage(_entity, eventMessage, rabbitMqMessageViewDto.QueueName, SystemInfo.Id, SystemInfo.Name);
            return Ok(result);
        }

    }
}